#include <windows.h>
#include <tchar.h>
#include "Main.h"
#include "MessageCallback.h"
#include "Constants.h"
#include "ErrorExit.h"
#include "FileOpener.h"
#include "resource.h"
#include "Settings.h"
using namespace constants;
using namespace SubPlayer;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
    #ifdef _DEBUG
    //Alloc Console//Alloc Console
    //print some stuff to the console
    //make sure to include #include "stdio.h"
    //note, you must use the #include <iostream>/ using namespace std
    //to use the iostream... #incldue "iostream.h" didn't seem to work
    //in my VC 6
    AllocConsole();
    FILE *file;
    freopen_s(&file, "conin$","r",stdin);
    freopen_s(&file, "conout$","w",stdout);
    freopen_s(&file, "conout$","w",stderr);
    SetConsoleOutputCP(1250);
    printf("Debugging Window:\n");
    #endif
    if (!InitApplication(hInstance)) {
        ErrorExit::ShowMessage(TEXT("InitApplication"), true);
    }
    
    HWND hwnd = CreateWindowEx(WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_NOACTIVATE | WS_EX_TOPMOST, APP_NAME, APP_NAME, 
        WS_MAXIMIZE | WS_VISIBLE | WS_POPUP |	WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, GetSystemMetrics(SM_CXFULLSCREEN),
        GetSystemMetrics(SM_CYFULLSCREEN), NULL, NULL, hInstance, NULL);
    if (hwnd == NULL) {
        ErrorExit::ShowMessage(TEXT("CreateWindowsEx"), true);
    }
    CreateNotificationIcon(hInstance, hwnd);
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int) msg.wParam;
}

ATOM InitApplication(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;                                


    wcex.cbSize         = sizeof(WNDCLASSEX);
    wcex.cbClsExtra     = 0;
    wcex.cbWndExtra     = 0;
    wcex.style          = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = WindowProc;
    wcex.hInstance      = hInstance;
    wcex.hIcon          = (HICON) LoadImageW(hInstance, L"BigIcon", IMAGE_ICON, 144, 108, LR_SHARED);
    wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground  = CreateSolidBrush(INVISIBLE_COLOR);
    wcex.lpszMenuName   = NULL;
    wcex.lpszClassName  = APP_NAME;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));

    return RegisterClassEx(&wcex);
}

void CreateNotificationIcon(HINSTANCE hInstance, HWND hwnd)
{
    NOTIFYICONDATA nid;
    
    nid.cbSize              = sizeof(NOTIFYICONDATA);
    nid.hWnd                = hwnd;
    nid.uID                 = NOTIFICATION_AREA_ID;
    nid.uFlags              = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    nid.uCallbackMessage    = WM_USER_NOTIF_ICON;
    nid.hIcon               = (HICON) LoadImageW(hInstance, L"SmallIcon", IMAGE_ICON, 22, 16, LR_SHARED);
    wcscpy_s(nid.szTip, NOTIFICATION_AREA_TIP);
    nid.uVersion            = NOTIFYICON_VERSION_4;
    
    if (!Shell_NotifyIcon(NIM_ADD, &nid)) {
        ErrorExit::ShowMessage(L"Shell_NotifyIcon", true);    
    }        
}